package com.volkanic.views;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.volkanic.controller.GameController;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Lucas on 22/07/2014.
 */
public class View implements Screen {

    /**
     * Tag for debugging
     */
    private final String TAG = "View";
    /**
     * The controller of the game
     */
    protected GameController _controller;
    /**
     * The batch to draw the view
     */
    protected SpriteBatch _batch;
    /**
     * The list of different stage of the screens to draw
     */
    protected List<Stage> _listStages;
    /**
     * The current stage of the game
     */
    protected GameController.StageState _currentState;

    /**
     * Create a new instance of the class SharedView
     * @param state the current state of the stage
     */
    public View(GameController controller, GameController.StageState state){
        _currentState = state;
        _controller = controller;
    }

    /**
     * Render the scene of stages to the screen
     * @param delta the delta of time between to rendering
     */
    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // does the drawing work
        _batch.begin();
        this.stagesDraw();
        _batch.end();
    }

    @Override
    public void resize(int width, int height) {
        // Gdx.app.debug(TAG, "Resize, not Implemented");
    }

    @Override
    public void show() {
        // Gdx.app.debug(TAG, "show, not Implemented");
    }

    @Override
    public void hide() {
        // Gdx.app.debug(TAG, "Hide, not Implemented");
    }

    @Override
    public void pause() {
        // Gdx.app.debug(TAG, "Pause, not Implemented");
    }

    @Override
    public void resume() {
        // Gdx.app.debug(TAG, "Resume, not Implemented");
    }

    @Override
    public void dispose() {
        Gdx.app.debug(TAG, "Dispose");
        synchronized (_listStages){
            if (_listStages != null){
                Iterator<Stage> iterator = _listStages.iterator();
                while(iterator.hasNext()){
                    Stage stage = iterator.next();
                    if (stage != null){
                        stage.clear();
                        stage.dispose();
                    }
                }
            }
        }
    }

    /**
     * Act each stage of the screen
     */
    public void stagesAct() {
        synchronized (_listStages){
            if (_listStages != null){
                Iterator<Stage> iteratorStages = _listStages.iterator();
                while (iteratorStages.hasNext()){
                    Stage stage = iteratorStages.next();
                    if (stage != null){
                        stage.act();
                    }
                }
            }
        }
    }

    /**
     * Draw each stage stage on the screen
     */
    private void stagesDraw() {
        synchronized (_listStages){
            if (_listStages != null){
                Iterator<Stage> iteratorStages = _listStages.iterator();
                while (iteratorStages.hasNext()) {
                    Stage stage =iteratorStages.next();
                    if (stage != null){
                        stage.draw();
                    }
                }
            }
        }
    }
}
